package com.estore.action;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.estore.converter.DateTime;
import com.estore.converter.ImageResize;
import com.estore.entity.Brand;
import com.estore.entity.Categories;
import com.estore.info.BrandInfo;
import com.estore.info.CatInfo;
import com.estore.info.ProductInfo;
import com.estore.info.SubCatInfo;
import com.estore.manager.ProductManager;
import com.estore.model.ProductModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends ActionSupport implements
		ModelDriven<ProductModel>, ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5625570598603354403L;
	ProductModel productModel = new ProductModel();
	ProductManager productManager;
	public HttpServletRequest request;

	// insert catname brandName to product
	public String insertProductInfo() {
		String catID = request.getParameter("catID");
		if (catID != null) {
			Integer id1 = Integer.parseInt(catID);
			if (id1 != null) {
				productModel.setCatInfo(productManager.getCategories(id1));
			}
		}
		listAllCategories();
		listAllBrand();
		return SUCCESS;
	}

	// update categories
	public String editCategories() {
		if (productModel.getCatInfo() != null) {
			CatInfo info = productModel.getCatInfo();
			productManager.updateCategories(info);
			List<SubCatInfo> listSubcat = productManager.listSubCatInfo();
			List<CatInfo> catInfo = productManager.listCatInfo();
			productModel.setListSubcat(listSubcat);
			productModel.setListCatInfo(catInfo);
		}
		return SUCCESS;
	}

	// get all cat/subcat
	public void listAllCategories() {
		List<SubCatInfo> listSubcat = productManager.listSubCatInfo();
		List<CatInfo> catInfo = productManager.listCatInfo();
		productModel.setListSubcat(listSubcat);
		productModel.setListCatInfo(catInfo);
	}

	// view categories
	public String viewCategories() {
		CatInfo catInfo = new CatInfo();
		String id = request.getParameter("catID");
		if (id != null) {
			Integer catID = Integer.parseInt(id);
			if (catID != null) {
				catInfo = productManager.getCategories(catID);
				productModel.setCatInfo(catInfo);
			}
		}
		listAllCategories();
		return SUCCESS;
	}

	// add new categories
	public String addCategories() {
		if (productModel.getCatInfo() != null) {
			CatInfo catInfo = productModel.getCatInfo();
			String catName = catInfo.getCatName();
			if (!productManager.checkCategories(catInfo.getCatName())) {
				productManager.insertCategories(productModel.getCatInfo());
				listAllCategories();
				productModel.setCatInfo(null);
				productModel.setErrorAddCat(null);
			} else {
				listAllCategories();
				productModel.setErrorAddCat("Danh mục đã tồn tại");
				productModel.setCatInfo(null);
				return ERROR;
			}
		}

		return SUCCESS;
	}

	// update Brand Detail
	public String editBrand() {
		BrandInfo info = productModel.getBrandInfo();
		if (info != null) {
			productManager.updateBrand(info.getBrandID(), info.getBrandName(),
					info.getWebsite());
			listAllBrand();
		}
		return SUCCESS;
	}

	public String getBrandInfo() {
		String id = request.getParameter("id");
		if (id != null) {
			Integer brandId = Integer.parseInt(id);
			if (brandId != null) {
				BrandInfo info = productManager.getBrandInfo(brandId);
				if (info != null) {
					productModel.setBrandInfo(info);
				}
			}
		}
		listAllBrand();

		return SUCCESS;
	}

	// list all Brand
	public void listAllBrand() {
		List<BrandInfo> listBrandInfo = productManager.getListBrand();
		if (listBrandInfo != null) {
			productModel.setListBrandInfo(listBrandInfo);
		}
	}

	public String addBrand() {
		try {
			productModel.setErrorAddBrand(null);
			BrandInfo info = productModel.getBrandInfo();
			if (info != null) {
				if (!productManager.checkBrandName(info.getBrandName())) {
					productManager.addBrand(info.getBrandName(),
							info.getWebsite());
					listAllBrand();
					productModel.setBrandInfo(null);
					return SUCCESS;
				} else {
					productModel.setErrorAddBrand("Brand đã tồn tại");
					listAllBrand();
					return ERROR;
				}
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// private List<ProductInfo> listallproduct;

	// list product by catID
	public String listProduct() {
		List<ProductInfo> listproduct = productManager
				.getListProduct(productModel.getCatID());
		if (listproduct != null) {
			productModel.setListproduct(listproduct);
		}
		return "success";
	}

	// list all Product
	public String listAllProduct() {
		productModel.setListproduct(productManager.getListAllProduct());
		return "success";
	}

	// add product
	public String addProduct() {
		productModel.setErrorAddProduct(null);
		String fileName = "";
		List<String> listFileName = new ArrayList<String>();
		DateTime dateTime = new DateTime();
		List<File> fileUpload = productModel.getImgUpload();
		List<String> fileUploadName = productModel.getImgUploadFileName();

		if (productModel.getProductName() != null
				&& productModel.getCatID() != 0
				&& productModel.getBrandID() != 0) {
			if (productManager.checkProduct(productModel.getProductName())) {
				productModel.setErrorAddProduct("Sản Phẩm đã tồn tại");
				addProductPageInfo();
				return ERROR;
			} else {
				if (fileUpload != null) {
					for (int i = 0; i < fileUpload.size(); i++) {
						fileName = dateTime.addImgDate()
								+ fileUploadName.get(i);
						String contextPath = request.getRealPath("/");
						String path = contextPath + "img/" + fileName;
						listFileName.add("img/" + fileName);
						try {
							File f = new File(path);
							BufferedImage originalImage = ImageIO
									.read(fileUpload.get(i));
							int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
									: originalImage.getType();
							BufferedImage resizeImage = ImageResize
									.resizeImage(originalImage, type);
							ImageIO.write(resizeImage, "jpg", f);
							// FileUtils.copyFile(fileUpload.get(i), f);
						} catch (IOException e) {
							System.out.println(e.getMessage());
						}
					}
				}
				ProductInfo info = new ProductInfo(
						productModel.getProductName(), productModel.getCatID(),
						productModel.getBrandID(), dateTime.currentDate(),
						listFileName);
				productManager.addProduct(info);
				productModel.setErrorAddProduct("Thêm Sản Phẩm thành công");
				addProductPageInfo();
				productModel.setProductName(null);
				return SUCCESS;
			}
		} else {
			productModel
					.setErrorAddProduct("Xin kiểm tra lại thông tin Sản Phẩm");
			addProductPageInfo();
			return ERROR;
		}
	}

	public void addProductPageInfo() {
		List<SubCatInfo> listSubcat = productManager.listSubCatInfo();
		List<CatInfo> catInfo = productManager.listCatInfo();
		productModel.setListSubcat(listSubcat);
		productModel.setListCatInfo(catInfo);
		List<BrandInfo> brandInfo = productManager.getListBrand();
		if (brandInfo != null) {
			productModel.setListBrandInfo(brandInfo);
		}
	}

	@Override
	public ProductModel getModel() {
		return productModel;
	}

	public void setModel(ProductModel productmodel) {
		this.productModel = productmodel;
	}

	public ProductModel getProductmodel() {
		return productModel;
	}

	public void setProductmodel(ProductModel productmodel) {
		this.productModel = productmodel;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
}
